package com.jiliason.segmentation.doMain.event;

import com.jiliason.segmentation.constant.enums.Err;
import com.jiliason.segmentation.constant.enums.Regex;
import com.jiliason.segmentation.entity.splitterStrategy.StrategyInPojo;
import com.jiliason.segmentation.entity.splitterStrategy.TypePojo;
import com.jiliason.segmentation.exception.ACSysException;
import com.jiliason.segmentation.utils.StrUtil;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class TypeSplitter {

    public static void main(String[] args) {
        TypeSplitter t = new TypeSplitter();
        String addrStr = "东区博德之大博德之dasdad博德之门为什么这么贵？";
        String name = "博德之";
        String regex = "[门|w]";
        String weba = "门为什么这么贵？";

        String s = "s";
        String[] split = addrStr.split(name);
        System.out.println(split);
        //t.useNameSplitType()
    }

    public TypePojo fieldSplitType(String fieldStr, Regex regex) throws ACSysException {

        List<String> Splitter = StrUtil.splittingStr(fieldStr, regex.v());
        if (Splitter.size() != 2) {
            boolean b = Regex.ar3.equals(regex) || Regex.ar4.equals(regex);
            if (b && Splitter.size() > 2) {
                String type = Splitter.get(Splitter.size() - 1);
                Splitter.set(Splitter.size() - 1, "");
                String name = String.join("", Splitter);
                return new TypePojo(name, type);
            }

            if (b && fieldStr.matches(regex.v())) {
                return new TypePojo(fieldStr, "");
            }

            throw new ACSysException("词库存在预期外的词。 =》 " + fieldStr + "|" + regex + ":" + regex.v(), Err.B);
        }
        return new TypePojo(Splitter.get(0), Splitter.get(1));
    }

    public String useNameSplitType(StrategyInPojo in, String name) {
        String addrStr = in.getAddrStr();
        String split = addrStr.split(name)[1];
        return FromStartGetType(split);
    }

    private String FromStartGetType(String split) {
        String[] regexes = new String[] {
                Regex.ar1.v(),
                Regex.ar2.v(),
                Regex.ar3.v(),
                Regex.ar4.v(),
        };

        for (String regex : regexes) {
            List<String> Splitter = StrUtil.splittingStr(split, regex);
            List<Boolean> t = Splitter.stream().map(string -> string.matches(regex)).toList();
            // {true, false}
            // 从开头split Type,且去合理type
            String type = "";
            for (int i = 0; i < t.size(); i++) {
                if (i == 0 && !t.get(i)) break;
                if (t.get(i)) {
                    type += t.get(i);
                } else return type;
            }
        }
        return "";
    }
}
